package com.dc.diffutil

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.util.*

class AsyncListDifferActivity : AppCompatActivity() {

    private lateinit var differAdapter: AsyncDifferAdapter
    private val jobArray = arrayOf("Android", "Java", "H5", "iOS", "UI", "产品", "运营", "测试")
    private val random = Random()
    private var list = mutableListOf<UserBean>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_async_list_differ)

        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.addItemDecoration(DividerItemDecoration(this, RecyclerView.VERTICAL))
        recyclerView.layoutManager = LinearLayoutManager(this)

        for (i in 0 until 8) {
            list.add(UserBean(i, "用户${i}", jobArray[0]))
        }
        differAdapter = AsyncDifferAdapter(this)
        recyclerView.adapter = differAdapter
        differAdapter.setData(list)

        findViewById<Button>(R.id.button).setOnClickListener {
            val newList = arrayListOf<UserBean>()
            for (i in 0 until 8) {
                val job: String = if (i % 3 == 0) {
                    jobArray[random.nextInt(jobArray.size)]
                } else {
                    jobArray[0]
                }
                if (list.size % 2 == 0) {
                    newList.add(2, UserBean(100, "用户100", jobArray[0]))
                }
            }
            list = newList
            differAdapter.setData(list)
        }
    }
}